<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>追加</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border: none;">
                <div class="nav" id="nav"></div>
                <div class="easyui-panel" style="border:none;width:100%;">
                    <form id="ff">
                        <div style="margin-bottom:20px;padding-left:20px;">
                            <label class="wrap-label" id="label-for-year">年：<input type="text" class="easyui-combobox" id="year" name="year" style="width:100px;"/></label>
                            <label class="wrap-label" id="label-for-month">月：<input type="text" class="easyui-combobox" id="month" name="month" style="width:100px;"/></label>
                            <label class="wrap-label" id="label-for-week">周：<input type="text" class="easyui-combobox" id="week" name="week" style="width:100px;"/></label>
                            <label class="wrap-label" id="label-for-matchtype">类型：</label>
                            <select class="easyui-combobox" data-options="panelHeight:'auto'" style="width:5%;" id="matchType" name="matchType" prompt="--请选择--">
                                <option value="" selected>全部</option>
                                <option value="COMBO" >套餐</option>
                                <option value="DISABLE" >禁用</option>
                                <option value="MATCH_MODEL" selected>手工</option>
                            </select>
                            <label class="wrap-label" id="label-for-categorycode">品类：</label>
                            <input type="text" id="categorySelect" name="categoryCode" class="easyui-combobox" data-options="panelHeight:'150'" style="width:7%;"/>
                            <label class="item-label">追加状态：</label>
                            <select id="selectAppendStatus" class="easyui-combobox" style="width: 6%" data-options="required:true,panelHeight:'auto'">
                                <option value="NONE" selected>未追加</option>
                                <option value="APPEND">已追加</option>
                                <option value="UNAPPEND">不追加</option>
                                <option value="APPEND_COMBO">追加套餐</option>
                                <option value="">全部</option>
                            </select>
                            <label class="wrap-label" id="label-for-brandcode">品牌：</label><input type="text" class="easyui-combobox" style="width:6%;" name="brandCode" id="brandCode"/>
                            <label class="wrap-label" id="label-for-modelCode">型号码：</label><input type="text" class="easyui-textbox" style="width:6%;" name="modelCode" id="modelCode"/>
                            <label class="wrap-label" id="label-for-originalModel">原始型号：</label><input type="text" class="easyui-textbox" style="width:6%;" name="originalModel" id="originalModel"/>
<!--                            <label class="item-label" >渠道码：</label>-->
<!--                            <input type="text" id="channelSelect" name="channelCode" class="easyui-textbox" style="width:93px;"/>-->
                            <a class="easyui-linkbutton c8" style="margin-left:40px;width:80px;" onclick="searchBy()">查询</a>
                        </div>
                    </form>
                </div>
                <div class="table-wrapper">
                    <table id="datagrid" data-options="fitColumns:true">
                        <thead>
                            <tr>
                                <th data-options="field:'id',align:'center'" checkbox="true"></th>
                                <th data-options="field:'categoryName',align:'center'" width="50">品类</th>
                                <th data-options="field:'brandCode',align:'center'" width="50">品牌码</th>
                                <th data-options="field:'brandName',align:'center'" width="50">品牌</th>
                                <th data-options="field:'modelCode',align:'center'" width="50">型号码</th>
                                <th data-options="field:'modelName',align:'center'" width="100">型号</th>
                                <th data-options="field:'originalModel',align:'center'" width="260">原始型号</th>

                                <th data-options="field:'matchType',align:'center',formatter:matchTypeFormatter" width="50">类型</th>
                                <th data-options="field:'appendStatus',align:'center',formatter:appendStatusFormatter" width="60">追加状态</th>
                                <th data-options="field:'action',formatter:actions,align:'center'" width="80">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div id="detailDiv" class="easyui-window" title="往期详情" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:400px;height:260px;padding:10px;">
            <h3>当期：</h3>
            <div id="content"></div>
            <h3>前一期：</h3>
            <div id="content1"></div>
            <h3>前二期：</h3>
            <div id="content2"></div>
            <h3>前三期：</h3>
            <div id="content3"></div>
        </div>
    </body>
    <script type="text/javascript">

        $(function () {
            setNav();
            var height = $(window).height();
            var nav_height = $("#nav")._outerHeight();
            var ff_height = $("#ff").height();
            var table_height = height - nav_height - ff_height - 20;
            $("#datagrid").css("max-height", table_height);
            $("#datagrid").datagrid({
                pagination: true,
                rownumbers: true,
                //url:"/webserver/suspectedHistoryMatchData/page",
                //method:"get",
                toolbar: [{
                    text: '追库',
                    handler: function(){append('APPEND','追库')}
                },'-', {
                    text: '不追库',
                    handler: function(){append('UNAPPEND','不追库')}
                },'-', {
                    text: '追加套餐',
                    handler: function(){append('APPEND_COMBO','追加套餐')}
                },'-'],
                loadFilter: function (data) {
                    $.messager.progress("close");
                    var page;
                    if (data.code == 200) {
                        page = data.data;
                    }else {
                        $.messager.alert("提示", data.msg, "error");
                        return ;
                    }

                    var res = {rows: page.content, total: page.totalElements};
                    var content = page.content;
                    var count = 0;
                    var rows = [];
                    var merges = [];
                    content.map(function (item, index) {
                        var arr = item.matchModels;
                        if (arr && arr.length > 1) {
                            merges.push({index: count, rowspan: arr.length});
                        }
                        if (arr && arr.length > 0) {
                            arr.map(function (item2) {
                                var record = {id: item.id, entityStatus: item.entityStatus, originalModel: item.originalModel, matchType: item.matchType, appendStatus: item.appendStatus};
                                record = $.extend(true, record, item2);
                                rows.push(record);
                                count++;
                            });
                        } else {
                            rows.push(item);
                            count++;
                        }
                    });
                    res.rows = rows;
                    res.merges = merges;
                    return res;
                },
                onLoadSuccess: function (data) {
                    var merges = data.merges;
                    for (var i = 0; i < merges.length; i++) {
                        $("#datagrid").datagrid("mergeCells", {
                            index: merges[i].index,
                            field: "originalModel",
                            rowspan: merges[i].rowspan
                        });
                    }
                },
                onBeforeCheck: function (index, row) {
                    if (row.append2HistoryMatchData == "APPEND") {
                        return false;
                    }
                    return true;
                }
            }).datagrid("getPager").pagination({
                onSelectPage: searchBy
            });
            initPage();
            aRecords("追加代换库","/webapp/pages/product/suspecteddata")
        });

        function initPage() {
            initUserCategorySelect("categorySelect",function (code) {
                initBrandSelectC("brandCode",null,null,code);
            });
            // initBrandSelect("brandCode");
            $.when(initMonthWeek("label-for-month", "label-for-week")).done(searchBy);
        }

        function searchBy(page, size) {
            var queryAppendStatus = $("#selectAppendStatus").combobox("getValue");
            var data = getFormData(document.getElementById("ff"));
            data['page'] = page || 1;
            data['size'] = $("#datagrid").datagrid("getPager").pagination("options").pageSize || size;
            data['reportType'] = window.parent.workMode.toUpperCase();
            data['appendStatus'] = queryAppendStatus;
            $.ajax({
                url: config.url + "/v2/suspectedHistoryMatchData/page",
                type: "get",
                data: transfer(data),
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 50,
                        text: "数据加载中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (data) {
                    $("#datagrid").datagrid("loadData", data);
                },
                error: function (data) {
                    $.messager.alert("提示", "查询异常", "error");
                }
            });
        }

        function append(status,content) {
            var selectedRow = $("#datagrid").datagrid("getChecked");
            if (selectedRow && selectedRow.length > 0) {
                var ids = selectedRow.map(function (item) {
                    return item.id;
                });
                $.messager.confirm("提示", "您确定" + content + "吗？", function (r) {
                    if (r) {
                        $.ajax({
                            url: config.url + "/v2/suspectedHistoryMatchData/appendStatus/" + status,
                            type: "POST",
                            data: JSON.stringify(ids),
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            xhrFields: {withCredentials: true},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "操作进行中..."
                                });
                            },
                            complete: function () {
                                $.messager.progress("close");
                            },
                            success: function (data) {
                                if (data.code == 200) {
                                    searchBy();
                                } else {
                                    $.messager.alert("提示", data.msg, "error");
                                }
                            },
                            error: function (data) {

                            }
                        });
                    }
                });

            } else {
                $.messager.alert("提示", "请选择一条记录", "warn");
            }
        }

        function matchTypeFormatter(val) {
            var res = "";
            switch (val) {
                case "DISABLE":
                    res = "禁用";
                    break;
                case "COMBO":
                    res = "套餐";
                    break;
                case "MATCH_MODEL":
                    res = "手工";
                    break;
                default:
                    res = val;
                    break;
            }
            return res;
        }

        function appendStatusFormatter(val) {
            var res = "";
            switch (val) {
                case "APPEND":
                    res = "已追加";
                    break;
                case "UNAPPEND":
                    res = "不追加";
                    break;
                case "NONE":
                    res = "未追加";
                    break;
                case "DISPUTE":
                    res = "争议";
                    break;
                case "APPEND_COMBO":
                    res = "追加套餐";
                    break;
                default:
                    res = val;
                    break;
            }
            return res;
        }

        $("#ff").keydown(function (event) {
            var e = event || window.event;
            var keyCode = e.keyCode || e.which;
            if (keyCode == "13") {
                searchBy();
            }
        });

        function actions(val, row){
            return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
                "style='width:60px;' onclick='detail(\"" + row.id +  "\")'>往期详情</a>";
        }

        function detail(id) {
            $.ajax({
                url: config.url + "/v2/suspectedHistoryMatchData/findDetail",
                type: "get",
                data: {id:id},
                dataType: "json",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 50,
                        text: "数据加载中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (data) {
                    if (data.code != 200) {
                        $.messager.alert("提示", data.msg, "error");
                    }else {
                        $("#content").text(data.msg.descript + "   [" + data.msg.price + "]");
                        $("#content1").text(data.msg.descript1 + "   [" + data.msg.price1 + "]");
                        $("#content2").text(data.msg.descript2 + "   [" + data.msg.price2 + "]");
                        $("#content3").text(data.msg.descript3 + "   [" + data.msg.price3 + "]");
                        $("#detailDiv").window("open");
                    }
                },
                error: function (data) {
                    $.messager.alert("提示", "查询异常", "error");
                }
            });

        }
    </script>
</html>